package com.deer.wms.ASN.manage.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.ASN.manage.constant.AsnManageConstant;
import com.deer.wms.ASN.manage.model.asn.AsnDetail;
import com.deer.wms.ASN.manage.model.asn.AsnDetailCriteria;
import com.deer.wms.ASN.manage.model.asn.AsnDetailDto;
import com.deer.wms.ASN.manage.model.asn.AsnMaster;
import com.deer.wms.ASN.manage.service.AsnDetailService;
import com.deer.wms.ASN.manage.service.AsnMasterService;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @author guo
 * @since 2020/01/03
 */
@Api(description = "ASN详情api接口")
@RestController
@RequestMapping("/asn/details")
public class AsnDetailController {

    @Autowired
    private AsnDetailService asnDetailService;
    @Autowired
    private AsnMasterService asnMasterService;


    @PostMapping("/insert")
    public Result add(@RequestBody AsnDetail asnDetail) {
        asnDetailService.save(asnDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        asnDetailService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody AsnDetail asnDetail) {
        asnDetailService.updateById(asnDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(Integer id) {
        AsnDetail asnDetail = asnDetailService.getById(id);
        return ResultGenerator.genSuccessResult(asnDetail);
    }

    @GetMapping("/list")
    public Result list(AsnDetailCriteria criteria) {
        List<Integer> notBillNums = Arrays.asList(82, 251300022, 251300022, 251300058, 200000013);

        String billNo = criteria.getBillNo();
        AsnDetail detail = asnDetailService.getOne(new QueryWrapper<AsnDetail>().eq("bill_no", billNo).last("limit 1"));
        AsnMaster asnMaster = asnMasterService.getOne(new QueryWrapper<AsnMaster>().eq("bill_no", billNo));
        if (asnMaster.getAsnType() == 10 && ObjectUtil.isNotEmpty(detail.getMtoNo())) {
            asnMasterService.synchronous(billNo);
        }

        if (ObjectUtil.isNotEmpty(asnMaster.getBillType()) &&!notBillNums.contains(asnMaster.getBillType())) {
            asnMasterService.synchronous(asnMaster);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AsnDetailDto> list = asnDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/qcList")
    public Result qcList(AsnDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AsnDetailDto> list = asnDetailService.findQcList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByDetailNo")
    public Result findByDetailNo(AsnDetailCriteria criteria) {
//        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AsnDetailDto> list = asnDetailService.findByDetailNo(criteria.getDetailNo());
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByBillNo")
    public Result findByBillNo(AsnDetailCriteria criteria) {
//        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AsnDetail> list = asnDetailService.list(new QueryWrapper<AsnDetail>().eq("bill_no", criteria.getBillNo().trim()));
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findByItemCode")
    public Result findByItemCode(AsnDetailCriteria criteria) {
        //FIXME:效率优化
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AsnDetailDto> list = asnDetailService.findList(criteria);
        List<AsnDetailDto> list2 = new ArrayList<>();
        for (AsnDetailDto asnDetailDto : list) {
            if (asnDetailDto.getState() < AsnManageConstant.ASN_STATE_ALL) {
                list2.add(asnDetailDto);
            }
        }
        PageInfo pageInfo = new PageInfo(list2);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/listByItemCode")
    public Result listByItemCode(AsnDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<Integer> states = new ArrayList<>();
        states.add(0);
        states.add(1);
        states.add(2);
        criteria.setStates(states);
        List<AsnDetailDto> list = asnDetailService.findList(criteria);
        Map<String, List<AsnDetailDto>> map = list.stream().collect(Collectors.groupingBy(AsnDetailDto::getAsnBillNo));
        List<AsnMaster> retData = new ArrayList<>();
        for (String key : map.keySet()) {
            retData.add(asnMasterService.getOneByField("bill_no", key));
        }
        PageInfo pageInfo = new PageInfo(retData);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
